const express = require("express");

const settings = require("./settings");
const heroHandler = require("./logic/hero");

const app = express();
const bodyParser = require('body-parser')

app.use(bodyParser.json()) // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })) // for parsing application/x-www-form-urlencoded

app.get("/hero", (request, response) => {
  res = heroHandler.getHero(request)

  response.send({
    status: 200,
    msg: "success",
    data: res,
  });
});

app.get("/hero/role", (request, response) => {
  // 英雄类型
  res = heroHandler.getHeroRole()

  response.send({
    status: 200,
    msg: "success",
    data: res,
  });
});

app.post("/hero", (request, response) => {
  // 新增英雄
  form = request.body
  heroHandler.createHero(form)

  response.send({
    status: 200,
    msg: "success"
  });
});

app.put("/hero/:heroId", (request, response) => {
  // 修改英雄
  heroId = request.params.heroId
  form = request.body
  heroHandler.updateHero(form, heroId)

  response.send({
    status: 200,
    msg: "success"
  });
});

app.delete("/hero/:heroId", (request, response) => {
  // 删除英雄
  heroId = request.params.heroId
  heroHandler.delHero(heroId)

  response.send({
    status: 200,
    msg: "success"
  });
});

app.listen(settings.PORT, () => {
  console.log("服务启动了: http://127.0.0.1:" + settings.PORT);
});
